<script setup>
import { ref } from "vue";
import {
  queryAllApi,
  addDeptApi,
  queryDeptByIdApi,
  updateDeptApi,
  delDeptByIdApi,
} from "@/api/dept";

const deptList = ref([]);
const openDialog = ref(false);
const deptData = ref({ name: "" });
const deptFormRef = ref();
const dialogTitle = ref("");
const rules = {
  name: [
    {
      required: true,
      message: "部门名称不能为空",
      trigger: "blur",
    },
    {
      min: 2,
      max: 10,
      message: "部门名称的长度应该在2-10位",
      trigger: "blur",
    },
  ],
};
const handleEdit = async (id) => {
  openDialog.value = true;
  dialogTitle.value = "编辑部门";
  if (deptFormRef.value) {
    deptFormRef.value.resetFields();
  }
  const { code, data } = await queryDeptByIdApi(id);
  if (code) {
    deptData.value = data;
  }
};
const handleDelete = async (id) => {
  await ElMessageBox.confirm("请问您确认要删除?", "温馨提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  });
  const { code, message } = await delDeptByIdApi(id);
  if (code) {
    ElMessage.success("删除成功！");
    queryAll();
  } else {
    ElMessage.error(message);
  }
};
const queryAll = async () => {
  const { data } = await queryAllApi();
  deptList.value = data;
};
const addDept = () => {
  openDialog.value = true;
  dialogTitle.value = "新增部门";
  deptFormRef.value?.resetFields();
};
const cancel = () => {
  deptData.value = { name: "" };
  openDialog.value = false;
};
const save = () => {
  // if (!deptData.value) return;
  deptFormRef.value.validate(async (valid) => {
    if (valid) {
      let code, message;
      if (!deptData.value.id) {
        const res = await addDeptApi(deptData.value);
        code = res.code;
        message = res.message;
      } else {
        const res = await updateDeptApi(deptData.value);
        code = res.code;
        message = res.message;
      }

      if (code) {
        deptData.value = { name: "" };
        openDialog.value = false;
        ElMessage.success(
          dialogTitle.value === "新增部门" ? "新增部门成功！" : "修改部门成功！"
        );

        queryAll();
      } else {
        ElMessage.error(message);
      }
    } else {
      ElMessage.error("表单校验不通过");
    }
  });
};
queryAll();
</script>

<template>
  <h1>部门管理</h1>
  <el-button type="primary" @click="addDept" class="button">新增部门</el-button>

  <el-table :data="deptList" stripe style="width: 100%">
    <el-table-column type="index" label="序号" width="280" />
    <el-table-column prop="name" label="部门名字" width="380" />
    <el-table-column prop="updateTime" label="更新时间" width="580" />
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button type="primary" @click="handleEdit(row.id)">编辑</el-button>
        <el-button type="danger" @click="handleDelete(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog v-model="openDialog" :title="dialogTitle" width="500">
    <el-form :model="deptData" :rules="rules" ref="deptFormRef">
      <el-form-item label="部门名称" label-width="80" prop="name">
        <el-input v-model="deptData.name" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="save"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
h1 {
  height: 40px;
}
.button {
  margin-bottom: 40px;
}
</style>
